package com.bizhongcaip.seller.ui.welcome;

import android.Manifest;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

import net.caixiaomi.info.MainActivity;
import net.caixiaomi.info.R;
import net.caixiaomi.info.app.CommonApp;
import net.caixiaomi.info.base.BaseActivity;
import net.caixiaomi.info.base.bgabanner.BGABanner;
import net.caixiaomi.info.base.permissions.Permission;
import net.caixiaomi.info.base.permissions.RxPermissions;
import net.caixiaomi.info.helper.AppHelper;
import net.caixiaomi.info.interfaces.SimpleEqualRatioTarget;
import net.caixiaomi.info.model.MineItem;
import net.caixiaomi.info.util.Constants;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.functions.Consumer;

public class GuideActivity extends BaseActivity {
    @BindView(R.id.banner)
    BGABanner mBanner;

    @BindView(R.id.btn_done)
    View mBtnDone;

    @OnClick(R.id.btn_done)
    void toDone() {
        CommonApp.STORE.set(Constants.KEY_FIRST_OPEN, false);
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        onBackPressed();
    }

    @Override
    protected int layout() {
        return R.layout.act_guide;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= 21) {
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }

        mBanner.setEnterSkipViewId(R.id.btn_done, 0);
        mBanner.setAdapter(new BGABanner.Adapter<View, MineItem>() {
            @Override
            public void fillBannerItem(BGABanner banner, View itemView, @Nullable MineItem model, int position) {
                ImageView imageView = (ImageView) itemView.findViewById(R.id.image);
                ImageView infoView = (ImageView) itemView.findViewById(R.id.info);
                infoView.setImageResource(position == 0 ? R.mipmap.ic_guide_info_1 : position == 1 ? R.mipmap.ic_guide_info_2 : R.mipmap.ic_guide_info_3);
//                Glide.with(infoView.getContext()).load(model.getIconId()).into(infoView);
                RequestOptions options = new RequestOptions().centerCrop();
                int width = AppHelper.getScreenWidth(CommonApp.getInstance());
                int height = (int) (AppHelper.getScreenHeight(CommonApp.getInstance()) - getResources().getDimension(R.dimen.app_bar_height));
//                Glide.with(itemView.getContext()).asBitmap().apply(options).load(model.getId()).into(new SimpleEqualRatioTarget(imageView, width, height));
                Glide.with(itemView.getContext()).load(model.getId()).into(imageView);
            }
        });
        List<MineItem> resId = new ArrayList<>();
        MineItem item = new MineItem();
        item.setId(R.mipmap.ic_guide_1);
        item.setIconId(R.mipmap.ic_guide_info_1);
        resId.add(item);
        item = new MineItem();
        item.setId(R.mipmap.ic_guide_2);
        item.setIconId(R.mipmap.ic_guide_info_2);
        resId.add(item);
        item = new MineItem();
        item.setId(R.mipmap.ic_guide_3);
        item.setIconId(R.mipmap.ic_guide_info_3);
        resId.add(item);
        mBanner.setData(R.layout.item_guide, resId, null);

        RxPermissions mRxPermissions = new RxPermissions(this);
        mRxPermissions
                .requestEach(
                        Manifest.permission.READ_PHONE_STATE, Manifest.permission.WRITE_EXTERNAL_STORAGE
                )
                .subscribe(new Consumer<Permission>() {
                    @Override
                    public void accept(Permission permission) throws Exception {
                        if (permission.granted) {
                        } else if (permission.shouldShowRequestPermissionRationale) {
                            // 用户拒绝了该权限，没有选中『不再询问』（Never ask again）,那么下次再次启动时，还会提示请求权限的对话框
                        } else {
                            // 用户拒绝了该权限，并且选中『不再询问』
                        }
                    }
                });
    }
}
